package handlers

import (
	"data_service/models"
	"data_service/server"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

// 初始化后台管理业务服务
var adminService = server.NewAdminService()

// AdminDashboardData 获取管理员仪表盘数据
func AdminDashboardData(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{"message": "欢迎来到管理员仪表盘"})
}

// GetSystemStats 获取系统统计数据
func GetSystemStats(c *gin.Context) {
	// 获取用户总数
	var userCount int64
	if err := models.GetDB().Model(&models.User{}).Count(&userCount).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "获取用户统计失败: " + err.Error()})
		return
	}

	// 获取动态总数
	var postCount int64
	if err := models.GetDB().Model(&models.Post{}).Count(&postCount).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "获取动态统计失败: " + err.Error()})
		return
	}

	// 获取房间总数
	var roomCount int64
	if err := models.GetDB().Model(&models.Room{}).Count(&roomCount).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "获取房间统计失败: " + err.Error()})
		return
	}

	// 返回统计数据
	c.JSON(http.StatusOK, gin.H{
		"user_count":  userCount,
		"post_count":  postCount,
		"room_count":  roomCount,
		"status":      "success",
	})
}

// GetAllUsers 获取所有用户列表（分页）
func GetAllUsers(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
	users, total, _ := adminService.GetAllUsers(page, pageSize)
	for i := range users {
		users[i].Password = ""
	}
	c.JSON(http.StatusOK, gin.H{
		"data":     users,
		"total":    total,
		"page":     page,
		"pageSize": pageSize,
	})
}

// UpdateUser 更新指定用户信息
func UpdateUser(c *gin.Context) {
	userId := c.Param("id")
	var req struct {
		Name   string `json:"name"`
		Email  string `json:"email"`
		Role   string `json:"role"`
		Status string `json:"status"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "无效的参数"})
		return
	}
	user, err := adminService.UpdateUser(userId, req.Name, req.Email, req.Role, req.Status)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "更新用户信息失败"})
		return
	}
	if user != nil {
		user.Password = ""
	}
	c.JSON(http.StatusOK, user)
}

// DeleteUser 删除指定用户
func DeleteUser(c *gin.Context) {
	userId := c.Param("id")
	if err := adminService.DeleteUser(userId); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "删除用户失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"message": "用户删除成功"})
}

// GetAllPostsAdmin 获取所有动态列表
func GetAllPostsAdmin(c *gin.Context) {
	posts, err := adminService.GetAllPostsAdmin()
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "获取动态列表失败"})
		return
	}
	c.JSON(http.StatusOK, posts)
}

// UpdatePostAdmin 更新指定动态
func UpdatePostAdmin(c *gin.Context) {
	postId := c.Param("id")
	var post models.Post
	if err := c.ShouldBindJSON(&post); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "无效的参数"})
		return
	}
	updated, err := adminService.UpdatePostAdmin(postId, &post)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "更新动态失败"})
		return
	}
	c.JSON(http.StatusOK, updated)
}

// DeletePostAdmin 删除指定动态
func DeletePostAdmin(c *gin.Context) {
	postId := c.Param("id")
	if err := adminService.DeletePostAdmin(postId); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "删除动态失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"message": "动态删除成功"})
}
